<template>
    <Form ref="formItem" :model="formItem" :label-width="120" :rules="ruleValidate">
        <FormItem label="学年编号" prop="schoolyear_code">
            <Input v-model="formItem.schoolyear_code" placeholder="学年编号"></Input>
        </FormItem>
        <FormItem label="学年名称" prop="schoolyear_name.zh">
            <Input v-model="formItem.schoolyear_name.zh" placeholder="学年名称"></Input>
        </FormItem>
        <FormItem label="学年状态" prop="status_flag">
            <Select v-model="formItem.status_flag">
                <Option :value="0" :key="0">过去</Option>
                <Option :value="1" :key="1">当前</Option>
                <Option :value="2" :key="2">未来</Option>
            </Select>
        </FormItem>
        <FormItem label="学年开始时间" prop="start_at">
            <DatePicker type="date" v-model="formItem.start_at" format="yyyy-MM-dd" :transfer="transfer"  placeholder="请选择学年开始日期" style="width:100%"></DatePicker>
        </FormItem>
        <FormItem label="学年结束时间" prop="end_at">
            <DatePicker type="date" v-model="formItem.end_at" format="yyyy-MM-dd" :transfer="transfer"  placeholder="请选择学年结束日期" style="width:100%"></DatePicker>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="submit('formItem')" :loading="btnload">{{ infoId?'修改':'添加' }}</Button>
        </FormItem>
    </Form>
</template>

<script>
export default {
    props: {
        infoId:Number, 
    },
    data () {
        return {
            formItem: {                    
                schoolyear_code:'',
                status_flag:'',
                start_at:'',
                end_at:'',
                schoolyear_name:{
                    zh:'',
                    mn:'', 
                    en:'',
                },
            },
            btnload:false,
            transfer:true,
            addUrl:'api/semester/v1/schoolyear/create',
            editUrl:'api/semester/v1/schoolyear/lists/',
            updateUrl:'api/semester/v1/schoolyear/update/',
            ruleValidate: {
                'schoolyear_name.zh': [
                    { required: true, message: '请输入学年名称', trigger: 'blur' }
                ],
                schoolyear_code: [
                    { required: true, message: '请输入学年编号', trigger: 'blur' }
                ],
                status_flag: [
                    { required: true, type: 'number', message: '请选择学年状态', trigger: 'change' }
                ],
                start_at: [
                    { required: true, type: 'date', message: '请选择学年开始日期', trigger: 'change' }
                ],
                end_at: [
                    { required: true, type: 'date', message: '请选择学年结束日期', trigger: 'change' }
                ],
            }
        }
    },
    methods: {
        /************************************************************
         * 
         * 提交
         * 
         ************************************************************/
        submit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.btnload = true
                    if (this.infoId!=null) {
                        this.editSubmit ()
                    } else {
                        this.addSubmit ()
                    }
                } else {
                    this.$Message.error('验证失败');
                    this.btnload = false 
                }
            })
        },
        
        /************************************************************
         * 
         * 执行添加数据
         * 
         ************************************************************/
        addSubmit () {
            this.$http.post(this.addUrl,this.formItem).then(val=>{
                this.$Message.success(val.message);
                this.$emit('requery',true)
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.btnload = false 
            })
        },
        
        /************************************************************
         * 
         * 执行修改数据
         * 
         ************************************************************/
        editSubmit () {
            this.$http.put(this.updateUrl+this.infoId,this.formItem).then(val=>{
                this.$Message.success(val.message);
                this.$emit('requery',true)  
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                    this.btnload = false 
                }
            })
        },
        
        /************************************************************
         * 
         * 单条数据查询（修改）
         * 
         ************************************************************/
        infodata(){
            this.$http.get(this.editUrl + this.infoId).then(val=>{ 
                let infoOld = JSON.parse(JSON.stringify(val.data))
                this.formItem.schoolyear_code = infoOld.schoolyear_code
                this.formItem.schoolyear_name = infoOld.schoolyear_name
                this.formItem.status_flag = parseInt(infoOld.status_flag)
                this.formItem.start_at = infoOld.start_at
                this.formItem.end_at = infoOld.end_at              
            }).catch(err => {
                console.log(err);
            })
        },
    },
    created () {
        if (this.infoId!=null){
            this.infodata ()
        } 
    },
}
</script>